package com.hzw.saas.service.rss.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.rss.IJournalArticleService;
import com.hzw.saas.api.rss.IJournalUserSubService;
import com.hzw.saas.api.rss.bo.JournalUserSubBO;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.common.util.SnowFlakeUtil;
import com.hzw.saas.service.rss.mapper.JournalUserSubMapper;
import com.hzw.saas.service.rss.model.JournalUserSub;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 期刊的用户订阅 服务实现类
 * </p>
 *
 * @author sonam
 * @since 2021-06-30
 */
@Service("journalUserSubService")
public class JournalUserSubServiceImpl extends ServiceImpl<JournalUserSubMapper, JournalUserSub> implements IJournalUserSubService, IService<JournalUserSub> {


    @Resource(name = "journalArticleService")
    private IJournalArticleService journalArticleService;

    @Override
    public JournalUserSubBO getById(String pid) {
        if(StrUtil.isBlank(pid)) return null;
        return MapperUtil.nf().map(super.getById(pid), JournalUserSubBO.class);
    }

    @Override
    public List<JournalUserSubBO> query(JournalUserSubBO journalUserSubBO) {
        if(Objects.isNull(journalUserSubBO)) return MapperUtil.nf().mapAsList(this.list(), JournalUserSubBO.class);
        JournalUserSub journalUserSub = MapperUtil.nf().map(journalUserSubBO, JournalUserSub.class);
        List<JournalUserSub> journalUserSubs = this.list(Wrappers.<JournalUserSub>query().setEntity(journalUserSub));
        return MapperUtil.nf().mapAsList(journalUserSubs, JournalUserSubBO.class);
    }

    @Override
    public void update(JournalUserSubBO journalUserSubBO) {
        if(journalUserSubBO == null) return;
        JournalUserSub journalUserAction = MapperUtil.nf().map(journalUserSubBO, JournalUserSub.class);
        this.updateById(journalUserAction);
    }

    @Override
    public void delete(JournalUserSubBO journalUserSubBO) {
        if(journalUserSubBO == null) return;
        this.remove(Wrappers.<JournalUserSub>query().setEntity(MapperUtil.nf().map(journalUserSubBO, JournalUserSub.class)));
    }

    @Override
    public void delete(String pid) {
        if(StrUtil.isBlank(pid)) return;
        this.removeById(pid);
    }

    @Override
    public List<JournalUserSubBO> findJournalUserSubByUserId(String userId){
        List<JournalUserSub> list = this.lambdaQuery().eq(JournalUserSub::getUserId, userId)
            .eq(JournalUserSub::getIsDelete, 0)
            .list();
        if(CollectionUtil.isNotEmpty(list)){
             return  MapperUtil.nf().mapAsList(list,JournalUserSubBO.class);
        }
        return new ArrayList<>();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addJournalUserSub(List<JournalUserSubBO> journalUserSubBOS) {
        if(CollectionUtil.isEmpty(journalUserSubBOS)) {
            return;
        }
        journalUserSubBOS.forEach(journalUserSubBO -> {
                journalUserSubBO.setPid(SnowFlakeUtil.nextIdStr());
        });
        saveBatch(MapperUtil.nf().mapAsList(journalUserSubBOS,JournalUserSub.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateJournalUserSub(List<JournalUserSubBO> journalUserSubBOS) {
        if(CollectionUtil.isEmpty(journalUserSubBOS)) {
            return;
        }
        updateBatchById(MapperUtil.nf().mapAsList(journalUserSubBOS,JournalUserSub.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<JournalUserSubBO> pushSubscribe(String userId){
        List<JournalUserSub> list = this.lambdaQuery().eq(JournalUserSub::getUserId, userId).list();
        return MapperUtil.nf().mapAsList(list, JournalUserSubBO.class);
    }

    @Override
    public List<String> findAlreadySub(List<String> collect1,String userId) {
        if (CollectionUtil.isNotEmpty(collect1)) {
            List<JournalUserSub> list = this.lambdaQuery().in(JournalUserSub::getJournalId, collect1)
                .eq(JournalUserSub::getUserId, userId)
                .groupBy(JournalUserSub::getJournalId)
                .list();
            if (CollectionUtil.isNotEmpty(list)){
               return list.stream().map(JournalUserSub::getJournalId).collect(Collectors.toList());
            }

        }
        return Collections.emptyList();
    }

}
